<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>http请求的几种方式 | 苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="http请求参数之Query String Parameters、Form Data、Request PayloadHTTP请求中不同的请求方式和设置不同的Content-Type时，参数传递的方式会不一样，一起了解这三种形式：Query String Parameters、Form Data、Request Payload GET请求Query String ParametersGET请求时，参">
<meta property="og:type" content="article">
<meta property="og:title" content="http请求的几种方式">
<meta property="og:url" content="http://dontworryneedmoney.gitee.io/guolianbo/2020/03/20/dontworry/%E6%9D%82%E9%A1%B9/http%E8%AF%B7%E6%B1%82%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/index.html">
<meta property="og:site_name" content="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒">
<meta property="og:description" content="http请求参数之Query String Parameters、Form Data、Request PayloadHTTP请求中不同的请求方式和设置不同的Content-Type时，参数传递的方式会不一样，一起了解这三种形式：Query String Parameters、Form Data、Request Payload GET请求Query String ParametersGET请求时，参">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-03-20T02:12:40.000Z">
<meta property="article:modified_time" content="2020-05-14T13:25:25.908Z">
<meta property="article:author" content="worry dont">
<meta property="article:tag" content="杂项">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/guolianbo/atom.xml" title="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/guolianbo/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/guolianbo/" id="logo">苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/guolianbo/">Home</a>
        
          <a class="main-nav-link" href="/guolianbo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/guolianbo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://dontworryneedmoney.gitee.io/guolianbo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="ejs-dontworry/杂项/http请求的几种方式" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/03/20/dontworry/%E6%9D%82%E9%A1%B9/http%E8%AF%B7%E6%B1%82%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/" class="article-date">
  <time datetime="2020-03-20T02:12:40.000Z" itemprop="datePublished">2020-03-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      http请求的几种方式
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="http请求参数之Query-String-Parameters、Form-Data、Request-Payload"><a href="#http请求参数之Query-String-Parameters、Form-Data、Request-Payload" class="headerlink" title="http请求参数之Query String Parameters、Form Data、Request Payload"></a>http请求参数之Query String Parameters、Form Data、Request Payload</h2><p>HTTP请求中不同的请求方式和设置不同的Content-Type时，参数传递的方式会不一样，一起了解这三种形式：Query String Parameters、Form Data、Request Payload</p>
<h3 id="GET请求"><a href="#GET请求" class="headerlink" title="GET请求"></a>GET请求</h3><p>Query String Parameters<br>GET请求时，参数会以url string 的形式进行传递，即?后的字符串则为其请求参数，并以&amp;作为分隔符。 General<br>请求参数拼接在url上用params的方式，axios请求方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">axios.delete(url,&#123;<span class="attr">params</span>:data&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>axios中put或者post使用params的方式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Qs= <span class="built_in">require</span>(<span class="string">'qs'</span>) ;</span><br><span class="line">putmodel:<span class="function">(<span class="params">data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">const</span> readyData = Qs.stringify(data);</span><br><span class="line">  <span class="keyword">return</span> putAxios(<span class="string">'/model'</span>+<span class="string">'?'</span>+ readyData);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Request URL: http:<span class="comment">//test.com?from_type=省&amp;from_name='四川省'</span></span><br><span class="line">Request Method: GET</span><br></pre></td></tr></table></figure>
<p>Query String Parameters</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">from_type=省&amp;from_name=‘四川省’</span><br></pre></td></tr></table></figure>

<h3 id="Post请求"><a href="#Post请求" class="headerlink" title="Post请求"></a>Post请求</h3><p>post请求会出现两种形式的请求体：</p>
<h4 id="FormData"><a href="#FormData" class="headerlink" title="FormData"></a>FormData</h4><p>当发起一次Post请求，若未指定Content-type，则默认content-type为application/x-www-form-urlencoded,即参数会以FormData的形式进行传递，不会显示出现在请求URL中。</p>
<p>axios设置Content-type的方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> axios = Axios.create(&#123;</span><br><span class="line">  baseURL: baseUrl,</span><br><span class="line">  timeout: <span class="number">30000</span>,</span><br><span class="line">  headers: &#123;</span><br><span class="line">    <span class="string">'Content-Type'</span>: <span class="string">'application/json; charset=utf-8'</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>请求参数放在请求体用 data</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">axios.delete(url,&#123;<span class="attr">data</span>:param&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">axios.post(url, data, &#123;<span class="attr">headers</span>: &#123;<span class="string">'Content-Type'</span>: <span class="string">'multipart/form-data'</span>&#125;&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="Request-Payload"><a href="#Request-Payload" class="headerlink" title="Request Payload"></a>Request Payload</h4><p>当发起一次post请求，若Content-Type为application/json，则参数会以Request Payload的形式进行传递（数据格式为json）,不会显示出现在请求url中。</p>
<h4 id="formData-方法"><a href="#formData-方法" class="headerlink" title="formData()方法"></a>formData()方法</h4><p>服务器为什么会对表单提交和文件上传做特殊处理，因为表单提交数据是名值对的方式，且Content-Type为application/x-www-form-urlencoded,而文件上传服务器需要特殊处理，普通的post请求（Content-Type不是application/x-www-form-urlencoded）数据格式不固定，不一定是名值对的方式，所以服务器无法知道具体的处理方式，所以只能通过获取原始数据流的方式来进行解析。</p>
<p>当我们遇到一些文件上传功能时，我们需要使用原生的formData()来进行数据组装，且content-type需要设置为multipart/formdata http请求头：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Request URL: http:<span class="comment">//test.com/upload</span></span><br><span class="line">Request Method: POST</span><br></pre></td></tr></table></figure>

<p>其中，WebKitFormBoundarysBkB6WoEBvbCRkmh为浏览器随机生成的boundary，作为分隔参数，作用等同于&amp;。</p>
<p>HTTP POST 表单请求提交时，使用的Content-Type是application/x-www-form-urlencoded，而使用原生AJAX的POST请求如果不指定请求头RequestHeader,默认使用的Content-Type是text/plain;charset=UTF-8。</p>
<p>所以，在使用原生AJAX POST请求时，需要明确设置Request Header,即：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">xhr.setRequestHeader(<span class="string">'Content-Type'</span>, <span class="string">'application/x-www-form-urlencoded'</span>;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/03/20/dontworry/%E6%9D%82%E9%A1%B9/http%E8%AF%B7%E6%B1%82%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/" data-id="ckb6hw6ry001xjcn2eiyxfs9f" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/guolianbo/2020/03/20/dontworry/%E6%9D%82%E9%A1%B9/npm%E5%AE%9E%E7%94%A8%E5%91%BD%E4%BB%A4/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          npm实用命令
        
      </div>
    </a>
  
  
    <a href="/guolianbo/2020/03/17/dontworry/%E9%9A%8F%E7%AC%94/%E8%BD%AF%E6%8A%80%E8%83%BD/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">软技能</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/guolianbo/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Unity3d/" rel="tag">Unity3d</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" rel="tag">消防</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" rel="tag">申论</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" rel="tag">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/guolianbo/tags/Algorithm/" style="font-size: 14px;">Algorithm</a> <a href="/guolianbo/tags/Unity3d/" style="font-size: 12px;">Unity3d</a> <a href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" style="font-size: 18px;">杂项</a> <a href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" style="font-size: 16px;">消防</a> <a href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" style="font-size: 10px;">申论</a> <a href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 20px;">随笔</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/02/">February 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/06/">June 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/guolianbo/2020/06/08/dontworry/Unity3d/IObserver%E5%92%8CIObservable%E5%92%8CIDisposable/">IObserver和IObservable和IDisposable</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/28/dontworry/%E6%9D%82%E9%A1%B9/javascript%E7%9A%84%E6%9C%AC%E8%B4%A8/">javascript的本质</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/27/dontworry/%E6%9D%82%E9%A1%B9/%E5%8D%8F%E7%A8%8B%E7%9A%84%E5%8E%9F%E7%90%86/">协程的原理</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/20/dontworry/%E6%9D%82%E9%A1%B9/linux%E4%B8%8B%E7%9A%84nginx%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">linux下的nginx的基本操作</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/15/dontworry/%E6%9D%82%E9%A1%B9/%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%8A%A8%E8%AF%8D/">常用函数动词</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 worry dont<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/guolianbo/" class="mobile-nav-link">Home</a>
  
    <a href="/guolianbo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/guolianbo/fancybox/jquery.fancybox.css">

  
<script src="/guolianbo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/guolianbo/js/script.js"></script>




  </div>
</body>
</html>